{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Numpy库(c语言开发的) 用于高性能科学计算和数据分析，是常用的高级数据分析库的基础包\n",
    "import numpy as np\n",
    "\n",
    "# 一维数组\n",
    "arr1 = np.array([1,2,3])\n",
    "arr2 = np.array([2,2,2])\n",
    "# 数学运算\n",
    "print('arr1+arr2 = ',arr1+arr2)\n",
    "print('arr1*arr2 = ',arr1*arr2)\n",
    "print('arr1 的数据类型是： ',arr1.dtype)\n",
    "\n",
    "# 二维数组\n",
    "data = [[1,1,1],[2,2,2,],[3,3,3]]\n",
    "arr3 = np.array(data)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成一维 0 数组，长度为10\n",
    "np.zeros(10)\n",
    "# 生成二维 0 数组，3*5\n",
    "np.zeros((3,5))\n",
    "\n",
    "# 生成3行4列的 1 数组\n",
    "np.ones((3,4))\n",
    "\n",
    "# 生成三维空值数组\n",
    "print(np.empty((2,3,3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切片\n",
    "\n",
    "# 生成数组 np.arange 类似python 的range函数\n",
    "arr4 = np.arange(10)\n",
    "print(arr4)\n",
    "# 切片\n",
    "print(arr4[3:5])\n",
    "\n",
    "# 切片赋值\n",
    "arr4[3:5] = 10\n",
    "print(arr4)\n",
    "\n",
    "# 复制\n",
    "arr5 = arr4[5:8].copy()\n",
    "arr5[:] = 8\n",
    "print(arr5) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandas import Series,DataFrame\n",
    "\n",
    "# 一维数组 Series, 与 np.array 的区别，Series 有索引，索引可重复\n",
    "obj = Series([4,5,6,-7])\n",
    "print(obj)\n",
    "print(obj.index)  # 默认索引是数字\n",
    "print(obj.values)\n",
    "\n",
    "# 指定索引\n",
    "obj2 = Series([2,4,6,7],index=['a','b','c','d'])\n",
    "# 通过索引赋值\n",
    "obj2['a'] = 1\n",
    "# 判断索引是否存在\n",
    "print('b' in obj2)\n",
    "print(obj2)\n",
    "\n",
    "# 字典转为 Series\n",
    "sdata = {\"beijing\":35000,\"shanghai\":71000,\"guangzhou\":16000,\"shenzhen\":5000}\n",
    "obj3 = Series(sdata)\n",
    "# 修改索引\n",
    "obj3.index = ['bj','sh','gz','sz']\n",
    "obj3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandas import Series,DataFrame\n",
    "\n",
    "# 多维数组 DataFrame, 类似电子表格的处理\n",
    "data = {'city':['sh','sh','sh','bj','bj'],\n",
    "        'year':[2018,2019,2020,2021,2022],\n",
    "        'pop':[1.5,1.7,3.6,2.4,2.9]}\n",
    "\n",
    "frame = DataFrame(data)\n",
    "\n",
    "# 按照列排序 columns\n",
    "frame2 = DataFrame(data,columns=['year','city','pop'])\n",
    "\n",
    "# 提取列数据 两种方式\n",
    "frame2['year']\n",
    "frame2.year\n",
    "\n",
    "\n",
    "# 新增列\n",
    "\n",
    "# 1.手工赋值\n",
    "frame2['new'] = 100\n",
    "\n",
    "# 2.通过其他列计算赋值，增加 cap 列，如果city=sh 则是 true，否则是false\n",
    "frame2['cap'] = frame2.city == 'sh'\n",
    "\n",
    "# 3.通过字典赋值，字典嵌套字典\n",
    "pop = {'bj':{2020:1.3,2021:1.5},'sh':{2020:2,2021:1.8}}\n",
    "frame3 = DataFrame(pop)\n",
    "print(frame3)\n",
    "\n",
    "# 行列互换\n",
    "frame3.T\n",
    "\n",
    "# 重新索引 reindex\n",
    "obj4 = Series([2,3,4,5],index=['a','b','c','d'])\n",
    "obj5 = obj4.reindex(['d','b','c','a'])\n",
    "print(obj5)\n",
    "\n",
    "# 空值赋值\n",
    "# 新增了一列 e ,填充为 0\n",
    "obj5 = obj4.reindex(['d','b','c','a','e'],fill_value=0)\n",
    "\n",
    "# 填充当前列的上一行或下一行的值\n",
    "obj6 = Series(['blue','purple','yellow'],index=[0,2,4])\n",
    "# 'ffill' 用上一行填充，'bfill'用下一行填充\n",
    "obj7 = obj6.reindex(range(6),method = 'ffill')\n",
    "obj7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import nan as NA\n",
    "# 删除缺失值\n",
    "\n",
    "data = Series([1,NA,2])\n",
    "# print(data.dropna())\n",
    "\n",
    "data2 = DataFrame([[1.,6.5,3],[1.,NA,NA],[NA,NA,NA]])\n",
    "# dropna() 删掉全部空值，\n",
    "# dropna(how='all') 删掉整行都为空值的行\n",
    "# dropna(axis=1,how='all') 删掉整列都为空值的列\n",
    "print(data2.dropna(how='all'))\n",
    "\n",
    "data2[4] = NA\n",
    "print(data2.dropna(axis=1,how='all'))\n",
    "\n",
    "# 将缺失值填充为 0 ,inplace=True 表示替换当前 data2，否则是修改副本\n",
    "data2.fillna(0,inplace=True)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a  1   -1.670695\n",
      "   2    2.270456\n",
      "   3   -0.043553\n",
      "b  1    1.812533\n",
      "   2    0.312670\n",
      "   3    0.455846\n",
      "c  1    0.695483\n",
      "   2   -0.293923\n",
      "d  2    0.938001\n",
      "   3    1.685875\n",
      "dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "a  1   -1.670695\n",
       "   2    2.270456\n",
       "   3   -0.043553\n",
       "b  1    1.812533\n",
       "   2    0.312670\n",
       "   3    0.455846\n",
       "c  1    0.695483\n",
       "   2   -0.293923\n",
       "d  2    0.938001\n",
       "   3    1.685875\n",
       "dtype: float64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 层次化索引\n",
    "data3 = Series(np.random.randn(10),\n",
    "        index=[['a','a','a','b','b','b','c','c','d','d'],\n",
    "        [1,2,3,1,2,3,1,2,2,3]])\n",
    "\n",
    "print(data3)\n",
    "\n",
    "# 转为二维数组\n",
    "data3.unstack()\n",
    "# 转为一维数组\n",
    "data3.unstack().stack()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f0831691c6bb6c483f09af1e7987fd76373eadc68cb706017f41b090bfe4d138"
  },
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
